************************************************
* Sleep Project - Pedro Bessone, Gautam Rao, Heather Schofield, Frank Schilbach, and Mattie Toma
* Purpose: Replicates Appendix Table 14 (Treatment Effects on Time Preferences)
* Last edited: 07 May 2021
************************************************

*************************************************
* Initial Set-up
*************************************************

	clear all
	set matsize 800
	set more off
	
************************************************
* Present Bias Regressions
************************************************

	use "$d/pb_dataset.dta", clear

*Generating sample indicator for censored structural beta

	*Generating winsorized beta
	*Explaining: this part will create a new variable which will store 
	*winsorized betas. In addition to that, we make missing betas 999999 here.
	*The regression means to create an indicator that this variable is inside
	*the CENSORED sample (352 participants). We only need to substitute the 10
	*missing betas baseline on the CENSORED sample by something, doesn't matter what 
	*(but its gotta be smaller than 2 otherwise it will get replaced below)
	
	*Finally, the beta we created here can be used to obtain the average winsorized beta baseline
	*for the raw sample, which will then be used below in the real regressions when the type is RESTRICTED RAW.
		foreach var in beta_post beta_base{
		gen `var'_wins = `var'
		replace `var'_wins = 2 if `var' > 2 & `var' != .
		replace `var'_wins = 2 if `var' > 2 & `var' != .
		}
		
		replace beta_base = -9999999 if beta_base==. 
				
		reghdfe beta_post treat_pool treat_nap i.age beta_base if type=="censored" & convergence_code_post==0, absorb(female) vce(robust)
		gen _in_censored_sample = e(sample)

		bys pid: egen in_censored_sample = max(_in_censored_sample)
	
		drop _in_censored_sample
		

eststo clear

	foreach type in "censored" "raw" "raw_restricted" {

	    preserve
			if "`type'"=="raw_restricted"{
				keep if in_censored_sample == 1
				keep if type=="raw"
				
				sum beta_base_wins if type=="raw"
				
				replace beta_base = r(mean) if beta_base ==.
			}
			else{
			keep if type == "`type'"
			keep if convergence_code_post == 0
			}
			if "`type'"=="censored"{
				replace beta_post = 2 if beta_post > 2 & beta_post != .
				replace beta_base = 2 if beta_base > 2 & beta_base != .
				
				sum beta_base_wins
				
				gen dummy_missing = (beta_base==-9999999)
				
				replace beta_base = r(mean) if beta_base==-9999999
			}

			* Regression
			if type != "raw" {
			reg beta_post treat_pool treat_nap i.age beta_base female dummy_missing, vce(robust)
			}
			else{
			reg beta_post treat_pool treat_nap i.age beta_base female              , vce(robust)
			}
			 
			local obs_`type' = e(N)
			
			* Storing regression coefficients
			qui lincom _b[treat_pool]
				local coef_ns_`type' = string(r(estimate),"%3.2f")
				local se_ns_`type' = string(r(se),"%3.2f")
				local p_ns_`type' = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))
			
			qui lincom _b[treat_nap]
				local coef_nap_`type' = string(r(estimate),"%3.2f")
				local se_nap_`type' = string(r(se),"%3.2f")
				local p_nap_`type' = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))
				
			qui lincom _b[beta_base]
				local coef_base_`type' = string(r(estimate),"%3.2f")
				local se_base_`type' = string(r(se),"%3.2f")
				local p_base_`type' = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))
			
			* Storing summary statistics
			if "`type'" == "censored" {
			qui sum beta_post if treat_pool ==0 & treat_nap == 0 
				local mean_`type' = string(r(mean), "%3.2f")
				local sd_`type' = string(r(sd), "%3.2f")
			}
			else {
			qui sum beta_post if treat_pool ==0 & treat_nap == 0 
				local mean_`type' = string(r(mean) + 1, "%3.2f")
				local sd_`type' = string(r(sd), "%3.2f")			
			}
				
				
			* Adding significance stars
			foreach treat in ns nap {
		
				if `p_`treat'_`type'' < 0.01 {
					local coef_`treat'_`type' = "`coef_`treat'_`type''***"
				}
			
				if `p_`treat'_`type'' < 0.05 & `p_`treat'_`type'' >= 0.01 {
					local coef_`treat'_`type' = "`coef_`treat'_`type''**"
				}
			
			    if `p_`treat'_`type'' < 0.1 & `p_`treat'_`type'' >= 0.05 {
					local coef_`treat'_`type' = "`coef_`treat'_`type''*"
			    }
				
			}
	
		restore
	
	}
	
*Version 2 only
foreach version_n in 2 {
	foreach type in "censored" "raw" "raw_restricted"  {

		preserve
			if "`type'"=="raw_restricted"{
				keep if in_censored_sample == 1
				keep if type=="raw"
			}
			else{
			keep if type == "`type'"
			keep if convergence_code_post == 0
			}
			keep if version == `version_n'
			
			if type=="censored"{
				replace beta_post = 2 if beta_post > 2 & beta_post != .
				replace beta_base = 2 if beta_base > 2  & beta_base != .
				
				sum beta_base_wins
				
				gen dummy_missing = (beta_base==-9999999)
				
				replace beta_base = r(mean) if beta_base==-9999999
			}
			if "`type'"=="raw_restricted"{
				keep if in_censored_sample == 1
			}
			
			* Regression			
			if type != "raw" {
				reg beta_post treat_pool treat_nap i.age beta_base dummy_missing female, vce(robust)
			}
			else{
				reg beta_post treat_pool treat_nap i.age beta_base female, vce(robust)
			}
			 
			
			local obs_`type'_`version_n' = e(N)
			
			* Storing regression coefficients 
			qui lincom _b[treat_pool]
				local coef_ns_`type'_`version_n' = string(r(estimate),"%3.2f")
				local se_ns_`type'_`version_n' = string(r(se),"%3.2f")
				local p_ns_`type'_`version_n' = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))
				
			qui lincom _b[treat_nap]
				local coef_nap_`type'_`version_n' = string(r(estimate),"%3.2f")
				local se_nap_`type'_`version_n' = string(r(se),"%3.2f")
				local p_nap_`type'_`version_n' = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))
				
			qui lincom _b[beta_base]
				local coef_base_`type'_`version_n' = string(r(estimate),"%3.2f")
				local se_base_`type'_`version_n' = string(r(se),"%3.2f")	
				local p_base_`type'_`version_n' = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))
				
			* Storing summary statistics
			if "`type'" == "censored" {
			qui sum beta_post if treat_pool ==0 & treat_nap == 0 
				local mean_`type'_`version_n' = string(r(mean), "%3.2f")
				local sd_`type'_`version_n' = string(r(sd), "%3.2f")
			}
			else {
			qui sum beta_post if treat_pool ==0 & treat_nap == 0 
				local mean_`type'_`version_n' = string(r(mean) + 1, "%3.2f")
				local sd_`type'_`version_n' = string(r(sd), "%3.2f")			
			}
				
			* Adding significance stars
			foreach treat in ns nap {
			
				if `p_`treat'_`type'_`version_n'' < 0.01 {
					local coef_`treat'_`type'_`version_n' = "`coef_`treat'_`type'_`version_n''***"
				}
			
				if `p_`treat'_`type'_`version_n'' < 0.05 & `p_`treat'_`type'_`version_n'' >= 0.01 {
					local coef_`treat'_`type'_`version_n' = "`coef_`treat'_`type'_`version_n''**"
				}
			
			    if `p_`treat'_`type'_`version_n'' < 0.1 & `p_`treat'_`type'_`version_n'' >= 0.05 {
					local coef_`treat'_`type'_`version_n' = "`coef_`treat'_`type'_`version_n''*"
			    }
				
			}
		
		restore	
}
}	

************************************************
* Savings Regressions
************************************************

	use "$d/savings_dataset.dta", clear

	*Regressions without interest
	
		local controls "female i.age risk_and_social_day default_amount rewardrate* max_pay_cog_tasks piece_rate_pb pid_interest"
		
		foreach i in daily_savings deposits {
		
			*generate baseline
			cap drop _baseline
			cap drop baseline
			gen _baseline = `i' if post_treatment==0
			egen baseline = mean(_baseline), by(pid)

			reghdfe `i' treat_pool treat_nap baseline `controls' ///
			if post_treatment == 1 , vce(cluster pid) absorb(i.surveyor i.date i.day_in_study, savefe) res
		
			local pids_`i' = string(e(N_clust))
			local obs_`i' = string(e(N))
			qui sum `i' if treat_pool == 0 & treat_nap == 0 & post_treatment == 1
			local mean_`i' = string(r(mean), "%3.2f")
			local sd_`i' = string(r(sd), "%3.2f")
			
			lincom _b[treat_pool]
			local coef_ns_`i' = string(r(estimate),"%3.2f")
			local se_ns_`i' = string(r(se),"%3.2f")
			local p_ns_`i' = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))
			
			lincom _b[treat_nap]
			local coef_nap_`i' = string(r(estimate),"%3.2f")
			local se_nap_`i' = string(r(se),"%3.2f")
			local p_nap_`i' = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))

			lincom _b[pid_interest]
			local coef_int_`i' = string(r(estimate),"%3.2f")
			local se_int_`i' = string(r(se),"%3.2f")
			local p_int_`i' = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))
			
			lincom _b[baseline]
			local coef_base_`i' = string(r(estimate),"%3.2f")
			local se_base_`i' = string(r(se),"%3.2f")
			local p_base_`i' = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))
			
			* Significance
			foreach j in ns nap int base{
				 if `p_`j'_`i'' < 0.01 {
					local coef_`j'_`i' = "`coef_`j'_`i''***"
				}
				
				if `p_`j'_`i'' < 0.05 & `p_`j'_`i'' >= 0.01 {
					local coef_`j'_`i' = "`coef_`j'_`i''**"
				}
				
				 if `p_`j'_`i'' < 0.1 & `p_`j'_`i'' >= 0.05 {
					local coef_`j'_`i' = "`coef_`j'_`i''*"
				}
			}
		}
		
	*Regressions with interest accrued - fake 1% interest
		local controls "female i.age risk_and_social_day default_amount rewardrate* max_pay_cog_tasks piece_rate_pb pid_interest"

		reghdfe interest_1_post treat_pool treat_nap total_interest_1_base `controls' ///
		if post_treatment == 1, vce(cluster pid) absorb(i.surveyor i.date i.day_in_study, savefe) res

		local pids_fake = string(e(N_clust))
		local obs_fake = string(e(N))
		
		qui sum interest_1_post if treat_pool == 0 & treat_nap == 0 & post_treatment == 1
		local mean_fake = string(r(mean), "%3.2f")
		local sd_fake = string(r(sd), "%3.2f")
			
		lincom _b[treat_pool]
		local coef_ns_fake = string(r(estimate),"%3.2f")
		local se_ns_fake = string(r(se),"%3.2f")
		local p_ns_fake = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))
		
		lincom _b[treat_nap]
		local coef_nap_fake = string(r(estimate),"%3.2f")
		local se_nap_fake = string(r(se),"%3.2f")
		local p_nap_fake = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))

		lincom _b[pid_interest]
		local coef_int_fake = string(r(estimate),"%3.2f")
		local se_int_fake = string(r(se),"%3.2f")
		local p_int_fake = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))
		
		lincom _b[total_interest_1_base]
		local coef_base_fake = string(r(estimate),"%3.2f")
		local se_base_fake = string(r(se),"%3.2f")
		local p_base_fake = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))
			
			* Significance
			foreach j in ns nap int base{
				 if `p_`j'_fake' < 0.01 {
					local coef_`j'_fake = "`coef_`j'_fake'***"
				}
				
				if `p_`j'_fake' < 0.05 & `p_`j'_fake' >= 0.01 {
					local coef_`j'_fake = "`coef_`j'_fake'**"
				}
				
				 if `p_`j'_fake' < 0.1 & `p_`j'_fake' >= 0.05 {
					local coef_`j'_fake = "`coef_`j'_fake'*"
				}
			}

	*Regressions with interest accrued - real interest and positive interest
	
		local controls "female i.age risk_and_social_day default_amount rewardrate* max_pay_cog_tasks piece_rate_pb pid_interest"

		reghdfe interest_post treat_pool treat_nap total_interest_base `controls' ///
		if post_treatment == 1 & pid_interest > 0 & pid_interest!=. ///
		, vce(cluster pid) absorb(i.surveyor i.date i.day_in_study, savefe) res
		
		local pids_real = string(e(N_clust))
		local obs_real = string(e(N))
		
		qui sum interest_post if treat_pool == 0 & treat_nap == 0 & pid_interest > 0 & pid_interest!=. & post_treatment == 1
		local mean_real = string(r(mean), "%3.2f")
		local sd_real = string(r(sd), "%3.2f")

		lincom _b[treat_pool]
		local coef_ns_real = string(r(estimate),"%3.2f")
		local se_ns_real = string(r(se),"%3.2f")
		local p_ns_real = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))
		
		lincom _b[treat_nap]
		local coef_nap_real = string(r(estimate),"%3.2f")
		local se_nap_real = string(r(se),"%3.2f")
		local p_nap_real = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))

		lincom _b[pid_interest]
		local coef_int_real = string(r(estimate),"%3.2f")
		local se_int_real = string(r(se),"%3.2f")
		local p_int_real = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))
		
		lincom _b[total_interest_base]
		local coef_base_real = string(r(estimate),"%3.2f")
		local se_base_real = string(r(se),"%3.2f")
		local p_base_real = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))
			
			* Significance
			foreach j in ns nap int base{
				 if `p_`j'_real' < 0.01 {
					local coef_`j'_real = "`coef_`j'_real'***"
				}
				
				if `p_`j'_real' < 0.05 & `p_`j'_real' >= 0.01 {
					local coef_`j'_real = "`coef_`j'_real'**"
				}
				
				 if `p_`j'_real' < 0.1 & `p_`j'_real' >= 0.05 {
					local coef_`j'_real = "`coef_`j'_real'*"
				}
			}
		
************************************************
* Generating Time Preferences Table
************************************************


cd "$oa/Tables"
		file open f using "TableA14a_time_preferences_part1_new.tex", write replace
			file write f "\begin{tabular*}{1\textwidth}{l@{\extracolsep{\fill}}*{8}{c}}" _n ///+
			"\toprule" _n ///
			"\textbf{\textit{Panel A: Savings}} \\" _n ///
			"&\multicolumn{2}{c}{\textbf{Savings}}&\multicolumn{2}{c}{\textbf{Interest Accrued}}\\" _n ///
			"\cmidrule(lr){2-3}\cmidrule(lr){4-5} & Deposits & Net Savings & Real Pos. Rates & Hypothetical 1\% \\" _n ///
			" & (1) & (2) & (3) & (4) \\" _n ///
			"\midrule" _n ///
			"Night-Sleep Treatments & `coef_ns_deposits' & `coef_ns_daily_savings' & `coef_ns_real' & `coef_ns_fake' \\" _n ///
			"& (`se_ns_deposits') & (`se_ns_daily_savings') & (`se_ns_real') & (`se_ns_fake') \\" _n ///
			"Nap Treatment & `coef_nap_deposits' & `coef_nap_daily_savings' & `coef_nap_real' & `coef_nap_fake' \\" _n ///
			"& (`se_nap_deposits') & (`se_nap_daily_savings') & (`se_nap_real') & (`se_nap_fake') \\" _n ///
			"Interest Rate & `coef_int_deposits' & `coef_int_daily_savings' & `coef_int_real' & `coef_int_fake'  \\" _n ///
			"& (`se_int_deposits') & (`se_int_daily_savings') & (`se_int_real') & (`se_int_fake')\\" _n ///
			"\midrule" _n ///
			"Control Mean & `mean_deposits' & `mean_daily_savings' & `mean_real' & `mean_fake' \\" _n ///
			"Control SD   & `sd_deposits'   & `sd_daily_savings'   & `sd_real'   & `sd_fake'   \\" _n ///
			"Participants & `pids_deposits' & `pids_daily_savings' & `pids_real' & `pids_fake' \\" _n ///
			"\\" _n ///
			"\end{tabular*}" _n 
			file close f
			
			
		file open f using "TableA14b_time_preferences_part2_new.tex", write replace
			file write f "\begin{tabular}{@{}c@{}}" _n ///
			"\begin{tabular}{l*{6}{c}}" _n ///
			"\textbf{\textit{Panel B: Present Bias}} \\" _n ///
			"&\multicolumn{2}{c}{\textbf{Structural Beta ($\beta$)}}&\multicolumn{4}{c}{\textbf{Ratio Now vs. Later}} \\" _n ///
			"\cmidrule(lr){2-3}\cmidrule(lr){4-7}" _n ///
			"&\multicolumn{2}{c}{}&\multicolumn{2}{c}{Restricted}&\multicolumn{2}{c}{Unrestricted}  \\" _n ///
			"\cmidrule(lr){4-5}\cmidrule(lr){6-7}& Full Sample & New Version  & Full Sample  & New Version & Full Sample & New Version \\" _n ///
			"& (1) & (2) & (3) & (4) & (5) & (6) \\" _n ///
			"\midrule" _n ///
			"Night-Sleep Treatments & `coef_ns_censored' & `coef_ns_censored_2' & `coef_ns_raw_restricted' & `coef_ns_raw_restricted_2' & `coef_ns_raw' & `coef_ns_raw_2' \\" _n ///
			"& (`se_ns_censored') & (`se_ns_censored_2') & (`se_ns_raw_restricted') & (`se_ns_raw_restricted_2')  & (`se_ns_raw') & (`se_ns_raw_2')  \\" _n ///
			"Nap Treatment   & `coef_nap_censored' & `coef_nap_censored_2' & `coef_nap_raw_restricted' & `coef_nap_raw_restricted_2' & `coef_nap_raw' & `coef_nap_raw_2' \\" _n ///
			"& (`se_nap_censored') & (`se_nap_censored_2') & (`se_nap_raw_restricted') & (`se_nap_raw_restricted_2') & (`se_nap_raw') & (`se_nap_raw_2') \\" _n ///
			"\midrule" _n ///
			"Control Mean         & `mean_censored' & `mean_censored_2' &  `mean_raw_restricted' &  `mean_raw_restricted_2' &  `mean_raw'  & `mean_raw_2' \\" _n ///
			"Control SD           & `sd_censored' & `sd_censored_2' &  `sd_raw_restricted'  & `sd_raw_restricted_2' &   `sd_raw'  & `sd_raw_2'   \\" _n ///
			"Participants            & `obs_censored'  & `obs_censored_2' & `obs_raw_restricted' & `obs_raw_restricted_2' &   `obs_raw' & `obs_raw_2'  \\" _n ///
			"\bottomrule " _n ///
			"\end{tabular}" _n ///
			"\end{tabular}" _n
		file close f
